package com.avic.rong.payUtil;

import android.app.Activity;
import android.os.Handler;
import android.text.TextUtils;

import com.avic.rong.R;
import com.avic.rong.payUtil.pay.WexinAliPayRsultCallBack;
import com.avic.rong.payUtil.pay.payAli.AliPay;
import com.avic.rong.payUtil.pay.payWeiXin.WeixinPay;
import com.avic.rong.utils.ToastTools;

import java.util.HashMap;

/**
 * @author oumin 包含，请求订单，成功以后的订单确认等
 */
public class PayRequestWarp implements PayConstant {

    private int mPayType = PAY_TYPE_ALI;
    private String mPayId = "1";
    private static PayRequestWarp instance;
    static Activity mActivity;

    PayVo mPayVo;
    static PayResultCallBack mPayRsultCallBack;

    public static PayRequestWarp getInstance(Activity activity, PayResultCallBack payRsultCallBack) {
        mActivity = activity;
        mPayRsultCallBack = payRsultCallBack;
        if (instance == null) {
            instance = new PayRequestWarp();
        }
        return instance;
    }

    public static PayRequestWarp getInstance(Activity activity) {
        mActivity = activity;
        if (instance == null) {
            instance = new PayRequestWarp();
        }
        return instance;
    }


    private void showPayStateDialog(int type) {
        hidePayStateDialog();
//        payRequest = new DialogPayzState(mActivity);
//        if (!mActivity.isFinishing())
//            payRequest.showDialog(type);
    }

    private void hidePayStateDialog() {
//        if (payRequest != null && payRequest.isShowing()) {
//            payRequest.dialogDismiss();
//        }
    }

    // 支付过程中弹出蒙层提示
//    private DialogPayzState payRequest;
    WexinAliPayRsultCallBack mWexinAliPayRsultCallBack = new WexinAliPayRsultCallBack() {

        @Override
        public void onSucceed() {
//            showPayStateDialog(DialogPayzState.TYPE_VERIFY);
//            QueryRequestState(mPayVo.getOrderId()+"");

        }

        @Override
        public void onFailure() {
            hidePayStateDialog();
            ToastTools.showToast(mActivity, R.string.pay_failed);
        }

        @Override
        public void onCancel() {
            hidePayStateDialog();
            ToastTools.showToast(mActivity, R.string.pay_cancel);
        }
    };

    private void startPay() {
        hidePayStateDialog();
        if (mPayVo == null || TextUtils.isEmpty(mPayVo.getSign())) {
            ToastTools.showToast(mActivity, "订单数据异常");
            return;
        }
        switch (mPayType) {
            case PAY_TYPE_ALI:// 支付宝支付类型

                AliPay aliPay = new AliPay((Activity) mActivity, mWexinAliPayRsultCallBack);
                aliPay.pay(mPayVo.getSign());
                break;
            case PAY_TYPE_WEIXIN:// 微信支付类型--支付结果
                WeixinPay weixinPay = new WeixinPay(mWexinAliPayRsultCallBack);
//                weixinPay.sendPayReq(mPayVo.getSign(), mActivity);
                break;
        }
    }

    /**
     * 请求订单
     *
     * @param payid
     * @param bodyMap
     * @param PayType
     */
    public void requestPayItemOrder(String payid, HashMap<String, String> bodyMap, final int PayType) {
//        RequestQueryNumber = 0;
//        mPayType = PayType;
//        mPayId = payid;
//
//        showPayStateDialog(DialogPayzState.TYPE_REQUEST);
//        RequestWrap commonRequestWrap = new RequestWrap(mActivity, InterfaceUrlDefine.MYQ_SERVER_MESSAGE_PAY_ORDER_TYPE, bodyMap, new RequestAbstraceCallBack() {
//
//            @Override
//            public void requestServerFailure(Context cxt) {
//                super.requestServerFailure(cxt);
//                hidePayStateDialog();
//            }
//
//            @Override
//            public void requestSuccess(CommonResultBody resultBody) {
//                // 请求成功
//                PayVo payVo = ((PayBody) resultBody).getBody();
//                mPayVo = payVo;
//                if (mPayVo != null || !StringUtil.isEmpty(mPayVo.getSign())) {
//                    startPay();
//                } else {
//                    ToastTools.showToast(mActivity, R.string.request_order_failed);
//                    hidePayStateDialog();
//                }
//            }
//
//            @Override
//            public void requestServerResponseFailure(Context cxt, ResultHeaderVo resultvo, CommonResultBody resultBody) {
//                hidePayStateDialog();
//                if (!ResultCodeUtil.REQUEST_TOKEN_OUTTIME.equals(resultvo.getResult()) && !ResultCodeUtil.REQUEST_NOT_LOGIN.equals(resultvo.getResult())) {
//                    super.requestServerResponseFailure(cxt, resultvo, resultBody);
//                }
//            }
//
//            @Override
//            public void requestFinish() {
//                super.requestFinish();
//            }
//        });
//        commonRequestWrap.postCommonRequest();
    }

    /**
     * 请求订单
     *
     * @param bodyMap
     * @param PayType
     */
    public void requestPayItemOrder(HashMap<String, String> bodyMap, String requedtType, final int PayType) {
//        RequestQueryNumber = 0;
//        mPayType = PayType;
//
//        showPayStateDialog(DialogPayzState.TYPE_REQUEST);
//        RequestWrap commonRequestWrap = new RequestWrap(mActivity, requedtType, bodyMap, new RequestAbstraceCallBack() {
//
//            @Override
//            public void requestServerFailure(Context cxt) {
//                super.requestServerFailure(cxt);
//                hidePayStateDialog();
//            }
//
//            @Override
//            public void requestSuccess(CommonResultBody resultBody) {
//                // 请求成功
//                PayVo payVo = ((PayBody) resultBody).getBody();
//                mPayVo = payVo;
//                if (mPayVo != null || !StringUtil.isEmpty(mPayVo.getSign())) {
//                    startPay();
//                } else {
//                    ToastTools.showToast(mActivity, R.string.request_order_failed);
//                    hidePayStateDialog();
//                }
//            }
//
//            @Override
//            public void requestServerResponseFailure(Context cxt, ResultHeaderVo resultvo, CommonResultBody resultBody) {
//                hidePayStateDialog();
//                if (!ResultCodeUtil.REQUEST_TOKEN_OUTTIME.equals(resultvo.getResult()) && !ResultCodeUtil.REQUEST_NOT_LOGIN.equals(resultvo.getResult())) {
//                    super.requestServerResponseFailure(cxt, resultvo, resultBody);
//                }
//            }
//
//            @Override
//            public void requestFinish() {
//                super.requestFinish();
//            }
//        });
//        commonRequestWrap.postCommonRequest();
    }

    /**
     * 查询购买状态
     *
     * @param orderid
     */
    public void QueryRequestState(final String orderid) {
        HashMap<String, String> bodyMap = new HashMap<String, String>();
        bodyMap.put("orderid", orderid);
//        RequestWrap commonRequestWrap = new RequestWrap(mActivity, InterfaceUrlDefine.MYQ_SERVER_MESSAGE_PAY_STATE_TYPE, bodyMap, R.string.progress_dialog_tip_type1, new RequestAbstraceCallBack() {
//
//            @Override
//            public void requestServerFailure(Context cxt) {
//                if (RequestQueryNumber < 1) {
//                    reQueryState(orderid);
//                } else {
//                    hidePayStateDialog();
//                    PayResultDialogUtils.showPayResults(mActivity, mPayId);
//                }
//            }
//
//            @Override
//            public void requestSuccess(CommonResultBody resultBody) {
//
//                PayVo payVo = ((PayBody) resultBody).getBody();
//                if (payVo != null && mPayRsultCallBack != null) {
//                    // 购买成功回调
//                    if ("1".equals(payVo.getOpenservice())) {
//                        hidePayStateDialog();
//                        if (!TextUtils.isEmpty(payVo.getAccount())) {
//                            getInfoPreUtil().setSpUserAccount(payVo.getAccount());
//                        }
//                        mPayRsultCallBack.onSucceed(payVo);
//                    } else if ("0".equals(payVo.getOpenservice())) {
//                        // 0 的时候重新查询
//                        if (RequestQueryNumber < 1) {
//                            reQueryState(orderid);
//                        } else {
//                            hidePayStateDialog();
//                            PayResultDialogUtils.showPayResults(mActivity, mPayId);
//                        }
//                    } else {
//                        if (RequestQueryNumber < 1) {
//                            reQueryState(orderid);
//                        } else {
//                            hidePayStateDialog();
//                            PayResultDialogUtils.showPayResults(mActivity, mPayId);
//                        }
//                    }
//                }
//
//            }
//
//            @Override
//            public void requestServerResponseFailure(Context cxt, ResultHeaderVo resultvo, CommonResultBody resultBody) {
//                if (!ResultCodeUtil.REQUEST_TOKEN_OUTTIME.equals(resultvo.getResult()) && !ResultCodeUtil.REQUEST_NOT_LOGIN.equals(resultvo.getResult())) {
//                    // 请求失败
//                    if (RequestQueryNumber < 1) {
//                        reQueryState(orderid);
//                    } else {
//                        hidePayStateDialog();
//                        super.requestServerResponseFailure(cxt, resultvo, resultBody);
//                        PayResultDialogUtils.showPayResults(mActivity, mPayId);
//                    }
//                }
//
//            }
//        });
//        commonRequestWrap.postCommonRequest();
    }


    public void UserLeftMoneyBuyTicket(HashMap bodyMap, boolean isShow, final PayResultCallBack mPayTicketRsultCallBack) {
//        RequestAbstraceCallBack mRequestAbstraceCallBack = new RequestAbstraceCallBack() {
//
//            @Override
//            public void requestServerFailure(Context cxt) {
//                super.requestServerFailure(cxt);
//                if (mPayTicketRsultCallBack != null)
//                    mPayTicketRsultCallBack.onFailure();
//            }
//
//            @Override
//            public void requestSuccess(CommonResultBody resultBody) {
//
//                PayVo payVo = ((PayBody) resultBody).getBody();
//                if (payVo != null) {
//                    if (!TextUtils.isEmpty(payVo.getAccount())) {
//                        getInfoPreUtil().setSpUserAccount(payVo.getAccount());
//                    }
//                    if (!TextUtils.isEmpty(payVo.getLevel())) {
//                        getInfoPreUtil().setSpUserLevel(payVo.getLevel());
//                    }
//                    if (mPayTicketRsultCallBack != null)
//                        mPayTicketRsultCallBack.onSucceed(payVo);
//
//                }
//
//            }
//
//            @Override
//            public void requestServerResponseFailure(Context cxt, ResultHeaderVo resultvo, CommonResultBody resultBody) {
//                if (!ResultCodeUtil.REQUEST_TOKEN_OUTTIME.equals(resultvo.getResult()) && !ResultCodeUtil.REQUEST_NOT_LOGIN.equals(resultvo.getResult())) {
//                    super.requestServerResponseFailure(cxt, resultvo, resultBody);
//                    if (mPayTicketRsultCallBack != null)
//                        mPayTicketRsultCallBack.onFailure();
//                }
//            }
//        };
//        RequestWrap commonRequestWrap;
//        if (isShow) {
//            commonRequestWrap = new RequestWrap(mActivity, InterfaceUrlDefine.MYQ_SERVER_MESSAGE_PAY_YUE_TYPE, bodyMap, R.string.progress_dialog_tip_type1, mRequestAbstraceCallBack);
//
//        } else {
//            commonRequestWrap = new RequestWrap(mActivity, InterfaceUrlDefine.MYQ_SERVER_MESSAGE_PAY_YUE_TYPE, bodyMap, mRequestAbstraceCallBack);
//
//        }
//        commonRequestWrap.postCommonRequest();
    }

    int RequestQueryNumber = 1;

    /**
     * 隔5秒重连 第三方支付成功后查询服务器失败重连
     *
     * @param orderid
     */
    private void reQueryState(final String orderid) {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                RequestQueryNumber++;
                QueryRequestState(orderid);
            }
        }, 5000);
    }
}
